package com.njust.RateMyTray_backend.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration // 配置类
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired // 自动注入
    private JwtAuthInterceptor jwtAuthInterceptor;

    @Autowired // 自动注入
    private CurrentUserMethodArgumentResolver currentUserMethodArgumentResolver;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtAuthInterceptor)
                .addPathPatterns("/api/**") // 拦截所有/api/开头的请求
                .excludePathPatterns(
                        "/api/users/login",
                        "/api/users/register",
                        "/api/windows/**",
                        "/api/dishes/**",
                        // 为每个资源分类单独开放访问权限
                        "/upload/avatars/**",
                        "/upload/comments/**",
                        "/upload/dishes/**",
                        "/upload/windows/**"
                );
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(currentUserMethodArgumentResolver);
    }

    @Override //将请求映射到对应的资源
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String uploadPath = "file:E:/Documents/Projects/RateMyTray/upload/";

        registry.addResourceHandler("/upload/avatars/**")
                .addResourceLocations(uploadPath + "avatars/");

        registry.addResourceHandler("/upload/comments/**")
                .addResourceLocations(uploadPath + "comments/");

        registry.addResourceHandler("/upload/dishes/**")
                .addResourceLocations(uploadPath + "dishes/");

        registry.addResourceHandler("/upload/windows/**")
                .addResourceLocations(uploadPath + "windows/");
    }
} 